/*-----------------------------------------------------------------------------------*/
/* Nuvoton Technology Corporation confidential                                       */
/*                                                                                   */
/* Copyright (c) 2008 by Nuvoton Technology Corporation                              */
/* All rights reserved                                                               */
/*                                                                                   */
/*-----------------------------------------------------------------------------------*/
/* File Contents:                                                                    */
/*   rtc_lib.c                                                                       */
/*            The RTC Library.                                                       */
/*                                                                                   */
/* Version:                                                                          */
/*            v0.0.1                                                                 */
/*                                                                                   */
/* Project:                                                                          */
/*                                                                                   */
/*                                                                                   */
/*                                                                                   */
/*-----------------------------------------------------------------------------------*/

#ifndef _RTC_LIB_
#define _RTC_LIB_

/*-----------------------------------------------------------------------------------*/
/* Macro, type and constant definitions                                              */
/*-----------------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------------*/
/* Global interface variables declarations                                           */                                                                            
/*-----------------------------------------------------------------------------------*/


/*-----------------------------------------------------------------------------------*/
/* Interface function declarations                                                   */
/*-----------------------------------------------------------------------------------*/

#include "wblib.h"

#include "nuc900_reg.h"

#define INIT_KEY     0xa5eb1357
#define WRITE_KEY    0xa965
#define WAIT_COUNT    10000
#define RTCYEAR2000    2000
#define RTC_FCR_REFERENCE 32761
#define RTC_LEAP_YEAR   	1
#define RTC_CLOCK_12        0 
#define RTC_CLOCK_24        1
#define RTC_AM              1
#define RTC_PM              2
#define RTC_ERR_ID    		0xFFFF0E00


/*----------------------------------------------------------------------------------------------*/
/* Real Time clock  Error Code                                                                  */
/*----------------------------------------------------------------------------------------------*/
#define RTC_ERR_CALENDAR_VALUE                      (RTC_ERR_ID | 0x01)
#define RTC_ERR_TIME_VALUE                          (RTC_ERR_ID | 0x02)
#define RTC_ERR_TIMESACLE_VALUE                     (RTC_ERR_ID | 0x03)
#define RTC_ERR_DWR_VALUE                           (RTC_ERR_ID | 0x04)
#define RTC_ERR_MIN_VALUE                           (RTC_ERR_ID | 0x05)
#define RTC_ERR_SEC_VALUE                           (RTC_ERR_ID | 0x06)
#define RTC_ERR_FCR_VALUE                           (RTC_ERR_ID | 0x07)
#define RTC_ERR_ENOTTY                              (RTC_ERR_ID | 0x08)   /* Command not support           */
#define RTC_ERR_ENODEV                              (RTC_ERR_ID | 0x09)   /* Interface number out of range */
#define RTC_ERR_EIO                                 (RTC_ERR_ID | 0x0A)   /* Read/Write error              */


/*----------------------------------------------------------------------------------------------*/
/* INTERRUPT SOURCE                                                                             */
/*----------------------------------------------------------------------------------------------*/
typedef enum
{
	ALARM_INT    =0x01,
	TICK_INT     =0x02,
	WAKEUP_INT   =0x04,
	ALL_INT      =0x07
}INT_SOURCE;
/*---------------------------------------------------------------------------------------------------------*/
/* Define Ioctl commands                                                                                   */
/*---------------------------------------------------------------------------------------------------------*/
typedef enum
{
	RTC_IOC_IDENTIFY_LEAP_YEAR  =  0,
	RTC_IOC_SET_TICK_MODE       =  1,
	RTC_IOC_GET_TICK            =  2,
	RTC_IOC_RESTORE_TICK        =  3,
//	RTC_IOC_SET_ALARM_MASK      =  4,
	RTC_IOC_ENABLE_INT          =  5,
	RTC_IOC_DISABLE_INT         =  6,
//	RTC_IOC_SET_CURRENT_TIME    =  7,
//	RTC_IOC_SET_ALAMRM_TIME     =  8,
	RTC_IOC_SET_FREQUENCY       =  9 
}RTC_CMD;
/*---------------------------------------------------------------------------------------------------------*/
/* Define for RTC Tick mode                                                                                */
/*---------------------------------------------------------------------------------------------------------*/
typedef enum
{
 	RTC_TICK_1_SEC       =         0,                            /* 1     sec                            */
 	RTC_TICK_1_2_SEC     =         1,                            /* 1/2   sec                            */
 	RTC_TICK_1_4_SEC     =         2,                            /* 1/4   sec                            */ 
 	RTC_TICK_1_8_SEC     =         3,                            /* 1/8   sec                            */
 	RTC_TICK_1_16_SEC    =         4,                            /* 1/16  sec                            */
 	RTC_TICK_1_32_SEC    =         5,                            /* 1/32  sec                            */ 
 	RTC_TICK_1_64_SEC    =         6,                            /* 1/64  sec                            */
 	RTC_TICK_1_128_SEC   =         7                             /* 1/128 sec                            */
}RTC_TICK;
/*---------------------------------------------------------------------------------------------------------*/
/* Define Time data struct & some parameters                                                               */
/*---------------------------------------------------------------------------------------------------------*/
typedef void (RTC_FUNC_T)(void);

typedef enum
{
	CURRENT_TIME    =    0,
	ALARM_TIME      =    1 
}TIME_SELECT;
/*---------------------------------------------------------------------------------------------------------*/
/* Define Day of week parameter                                                                            */
/*---------------------------------------------------------------------------------------------------------*/
typedef enum
{
    SUNDAY         =   0,
    MONDAY         =   1,
    TUESDAY        =   2,
    WEDNESDAY      =   3,
    THURSDAY       =   4,
    FRIDAY         =   5,
    SATURDAY       =   6
}DWR_PARAMETER;
/*---------------------------------------------------------------------------------------------------------*/
/* Define Time Data Struct                                                                                 */
/*---------------------------------------------------------------------------------------------------------*/
typedef struct
{
    UINT8 ucClockDisplay;                                         /* 12-hr, 24-hr                          */
    UINT8 ucAmPm;                                                 /* Only 12-hr used                       */
    UINT32 ucSecond;
    UINT32 ucMinute;
    UINT32 ucHour;
    UINT32 ucDayOfWeek;
    UINT32 ucDay;
    UINT32 ucMonth;
    UINT32 uYear;
    RTC_FUNC_T *fnAlarmCallBack;    
}RTC_TIME_DATA_T;



/*---------------------------------------------------------------------------------------------------------*/
/* Define Tick Struct                                                                                      */
/*---------------------------------------------------------------------------------------------------------*/
typedef struct
{
    UINT8 ucMode;
    RTC_FUNC_T *fnTickCallBack;
}RTC_TICK_T;



/*---------------------------------------------------------------------------------------------------------*/
/* Define Function Prototype                                                                               */
/*---------------------------------------------------------------------------------------------------------*/
INT RTC_Init(VOID);   
INT RTC_Open(RTC_TIME_DATA_T *pt);
INT RTC_Ioctl(INT nNum, RTC_CMD uCmd, UINT32 uArg0, float uArg1);
INT RTC_Read(TIME_SELECT time, RTC_TIME_DATA_T *pt);
INT RTC_Write(TIME_SELECT time, RTC_TIME_DATA_T *pt);
INT RTC_Close(VOID);
#endif /* _RTC_LIB_                                                                                        */
